package com.crr.mall.product.service.impl;


import com.crr.mall.common.utils.Assert;
import com.crr.mall.product.dao.CategoryBrandRelationDao;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crr.mall.common.utils.PageUtils;
import com.crr.mall.common.utils.Query;

import com.crr.mall.product.dao.BrandDao;
import com.crr.mall.product.entity.BrandEntity;
import com.crr.mall.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                new QueryWrapper<BrandEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 分页查询列表
     *
     * @param map
     * @return
     */
    @Override
    public PageUtils queryByCondition(Map map) {
        int page = Integer.parseInt(map.get("page").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        PageHelper.startPage(page, pageSize);
        List<Map> list = baseMapper.queryByCondition(map);
        PageInfo info = new PageInfo(list);
        return new PageUtils(list, (int) info.getTotal(), pageSize, page);
    }

    /**
     * 品牌信息修改 级联的分类品牌关联表修改
     * @param entity
     * @return
     */
    @Transactional
    @Override
    public boolean updateById(BrandEntity entity) {
        boolean b = super.updateById(entity);
        Assert.fail(b,"修改品牌信息失败");
        if (null != entity.getName()) {
            int i = categoryBrandRelationDao.updateBrandNameByBrandId(entity.getBrandId(), entity.getName());
            Assert.fail(i==1,"修改品牌分类关联信息失败");
        }
        return true;
    }
}